package com.ilink.teacherservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ilink.ilinkcommon.domain.PO.studyservice.StudyTeacherCoursesFavorite;
import com.ilink.teacherservice.entity.PO.study.tree.StudyTreeFavorite;
import com.ilink.ilinkcommon.utils.SnowflakeIdGenerator;
import com.ilink.teacherservice.mapper.study.tree.StudyTreeFavoriteMapper;
import com.ilink.teacherservice.service.StudyTreeFavoriteService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@RequiredArgsConstructor
public class StudyTreeFavoriteServiceImpl extends ServiceImpl<StudyTreeFavoriteMapper, StudyTreeFavorite>
    implements StudyTreeFavoriteService {

    private final StudyTreeFavoriteMapper studyTreeFavoriteMapper;

    @Override
    @Transactional
    public String insertFavoriteTree(String userId, Long treeId) {
        try {
            // 检查是否已经存在相同的 userId 和 treeId
            Integer count = studyTreeFavoriteMapper.selectCount(
                new LambdaQueryWrapper<StudyTreeFavorite>()
                    .eq(StudyTreeFavorite::getUserId, userId)
                    .eq(StudyTreeFavorite::getId, treeId)
            );

            // 如果已存在则不插入，直接返回相应信息
            if (count > 0) {
                log.warn("User '{}' 已收藏 treeId '{}'，跳过插入", userId, treeId);
                return "用户已收藏该技能树，无法重复收藏";
            }

            // 雪花算法生成的ID
            SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1);
            Long favoriteId = idGenerator.nextId();

            StudyTreeFavorite studyTreeFavorite = new StudyTreeFavorite();
            studyTreeFavorite.setFavoriteId(favoriteId);
            studyTreeFavorite.setUserId(userId);
            studyTreeFavorite.setId(Long.valueOf(treeId));

            studyTreeFavoriteMapper.insert(studyTreeFavorite);
            return "收藏成功";
        } catch (Exception e) {
            log.error("insertFavoriteTree函数出错：" + e.getMessage());
            return "收藏失败，错误信息：" + e.getMessage();
        }
    }


    /**
     * @Author 郑千鹤
     * @Description //取消技能树收藏
     * @Date 15:28 2024/9/18
     * @Param [userId, courseId]
     * @return java.lang.String
     **/

    @Override
    @Transactional
    public String removeFavoriteTree(String userId, String treeId) {
        try {
            // 查询是否存在该收藏记录
            Long count = Long.valueOf(studyTreeFavoriteMapper.selectCount(
                new LambdaQueryWrapper<StudyTreeFavorite>()
                    .eq(StudyTreeFavorite::getUserId, userId)
                    .eq(StudyTreeFavorite::getId, Long.valueOf(treeId))
            ));

            // 如果不存在，则返回相应信息
            if (count == 0) {
                log.warn("User '{}' 没有收藏 treeId '{}'，无法取消收藏", userId, treeId);
                return "用户未收藏该技能树，无法取消收藏";
            }

            // 删除收藏记录
            studyTreeFavoriteMapper.delete(
                new LambdaQueryWrapper<StudyTreeFavorite>()
                    .eq(StudyTreeFavorite::getUserId, userId)
                    .eq(StudyTreeFavorite::getId, Long.valueOf(treeId))
            );

            return "取消收藏成功";
        } catch (Exception e) {
            log.error("removeFavoriteTree函数出错：" + e.getMessage());
            return "取消收藏失败，错误信息：" + e.getMessage();
        }
    }


    @Override
    public List<StudyTreeFavorite> getFavoriteListByUserId(String userId) {
        return studyTreeFavoriteMapper.selectList(
                new LambdaQueryWrapper<StudyTreeFavorite>()
                        .eq(StudyTreeFavorite::getUserId, userId)
        );
    }

}




